﻿using Collections.Pooled;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Domain.Shared.ValidateAttribute
{
    /// <summary>
    /// 性别校验
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
    public class SexValidateAttribute : ValidationAttribute
    {
        private PooledSet<string> Sexs = new PooledSet<string> { "Man","Woman","Unknow" };

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                this.ErrorMessage = "请选择性别";
                return false;
            }

            if (Sexs.Contains(value.ToString()))
            {
                return true;
            }
            this.ErrorMessage = "性别有误";
            return false;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                return new ValidationResult("请选择性别");
            }

            if (Sexs.Contains(value.ToString()))
            {
                return ValidationResult.Success;
            }
            return new ValidationResult("性别有误");
        }

    }
}
